<?php
/**
 * Created by PhpStorm.
 * User: Duke
 * Date: 2019/11/22
 * Time: 10:36
 */
namespace App\Http\Controllers\Admin;

use App\Exceptions\CoustomerException;
use App\Http\Logic\admin\ArticleLogic;
use App\Http\Logic\admin\ArticleCategoryLogic;
use Illuminate\Http\Request;


class ArticleController extends AlphaController{
    //首页
    public function index(){
        return view('admin/article/index');
    }
    //获取列表信息
    public function getInfo(){
        try{
            $article = new ArticleLogic();
            $list = $article->articleAllList();
            $ret = ['code'=>0,'msg'=>'成功','data'=>$list];
        }catch (CoustomerException $e){
            $ret = ['code'=>$e->getCode(),'msg'=>$e->getMessage()];
        }
        return $ret;
    }
    //添加
    public function add(){
        $article_category = new ArticleCategoryLogic();
        $articlecategorylist = $article_category->getCategoryList('0');
        return view('admin/article/add',['articlecategory' =>  $articlecategorylist]);
    }
    //编辑
    public function edit(){
        // 查询文章内容
//        $article = new ArticleLogic();
//        $articleInfo = $article->findArticleInfo($id);
        //查询文章列表分类
        $article_category = new ArticleCategoryLogic();
        $articlecategory = $article_category->getCategoryList('0');
        return view('admin/article/edit',[ 'articlecategory' =>  $articlecategory]);
    }
    //保存or添加
    public function save(){
        $article_info = new ArticleLogic();
        $articleData = json_decode($_POST['data'],true);
        // 1 添加 否则 更新
        if($_POST['type'] == "1"){
            $info = $article_info->insertArticle($articleData);
        }else{
            $info = $article_info->updateArticle($articleData);
        }
        $ret = ['code'=>0,'msg'=>'成功','data'=>$info];
        return json_encode($ret);
    }
    //删除
    public function del(){
        $article_info = new ArticleLogic();
        $i = json_decode($_POST['data'],true);
        $article_info->delArticle($i['id']);
        $ret = ['code'=>0,'msg'=>'成功'];
        return json_encode($ret);
    }
    // 文章回收站
    public function recycle(){
        return view('admin/article/recycle');
    }
    // 文章回收站列表
    public function recycleList(){
        try{
            $article = new ArticleLogic();
            $username = session('username');
            $list = $article->getConditionList($username);
            $ret = ['code'=>0,'msg'=>'成功','data'=>$list];
        }catch (CoustomerException $e){
            $ret = ['code'=>$e->getCode(),'msg'=>$e->getMessage()];
        }
        return $ret;
    }
    //评论
    public function comment(){
        return view('admin/article/comment');
    }
    // 评论列表
    public function commentList(){
        try{
            $article = new ArticleLogic();
            $list = $article->commentListLogic(1);
            $ret = ['code'=>0,'msg'=>'成功','data'=>$list];
        }catch (CoustomerException $e){
            $ret = ['code'=>$e->getCode(),'msg'=>$e->getMessage()];
        }
        return $ret;
    }
}
